#include <ximera/system/application.hpp>
#include <ximera/logger.hpp>
#include <ximera/xml/xmldocument.hpp>
#include <ximera/xml/xmliterator.hpp>
#include <ximera/xml/xmlelement.hpp>

#include <iostream>

struct InputTestHandler : ximera::system::AppHandler
{
	virtual void onStart(ximera::system::AppEventArgs& args)
    {
        try
        {
            testXml(args.app);
        }
        catch (ximera::Exception& e)
        {
            args.app.shouldClose(true);
            XIMERA_LOG() << e << std::endl;
        }
    }

private:
    void testXml(ximera::system::Application& app)
    {
        using namespace ximera;
        xml::XmlDocument doc;
        doc.loadXml(L"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            L"<rootnode>"
            L"  <child1 attr1=\"culo\" />"
            L"  <child2>ano</child2>"
            L"  <child2>ano343</child2>"
            L"  <child1 attr1=\"culo\" />"
            L"  <child1 />"
            L"  <child2>ano5<b>asa</b>cuca</child2>"
            L"  <child2>ano123</child2>"
            L"</rootnode>");
        
        //doc.loadXml(L"<?xml version=\"1.0\" encoding=\"utf-8\"?>");

        for (xml::XmlNode::ChildIterator it = doc.firstChild()->childBegin(); it != doc.firstChild()->childEnd(); ++it)
        {
            XIMERA_LOG() << "node: " << (*it)->value() << " type: " << static_cast<int>((*it)->type()) << std::endl;
        }

        XIMERA_LOG() << "-----\n";

        for (xml::XmlIterator<xml::XmlElement> it(doc.rootElement(), L"child2"); it; ++it)
        {
            XIMERA_LOG() << "node: " << it->value() << " type: " << static_cast<int>(it->type()) << std::endl;
            XIMERA_LOG() << "   text: " << it->retrieveText() << std::endl;
        }
    }
};

int main()
{
    using namespace ximera;

    try
    {
        XIMERA_LOG().stream(&std::wcout);

        system::Application app;

        InputTestHandler handler;
        app.addHandler(handler);

        return app.run();
    }
    catch (ximera::Exception& e)
    {
        XIMERA_LOG() << e << std::endl;
    }
}
